#include "scheduler/abstract_scheduler.h"

#include <utility>

namespace scheduler
{
AbstractScheduler::AbstractScheduler(const std::string &name)
    : scheduler_name_(name)
{

}
AbstractScheduler::~AbstractScheduler() = default;

bool AbstractScheduler::appendTask(const task::AbstractTaskConstPtr &task)
{
    return schedule_policy_->addTask(task);
}
bool AbstractScheduler::appendScheduler(const AbstractSchedulerConstPtr &scheduler)
{
    scheduler_vec_.push_back(scheduler);
    return schedule_policy_->addPolicy(scheduler->getTaskPolicy());
}
bool AbstractScheduler::setTask(const task::AbstractTaskConstPtr &task)
{
    return schedule_policy_->setTask(task);
}
bool AbstractScheduler::setScheduler(const AbstractSchedulerConstPtr &scheduler)
{
    scheduler_vec_.push_back(scheduler);
    return schedule_policy_->setPolicy(scheduler->getTaskPolicy());
}
policy::AbstractPolicyConstPtr AbstractScheduler::getTaskPolicy()
{
    return schedule_policy_;
}
void AbstractScheduler::setContext(task::Context *context)
{
    schedule_policy_->setContext(context);
}
std::string AbstractScheduler::toString()
{
    return schedule_policy_->toString();
}
std::string AbstractScheduler::getName() const
{
    return scheduler_name_;
}
void AbstractScheduler::run()
{
    schedule_policy_->run();
}
void AbstractScheduler::pause()
{
    schedule_policy_->pause();
}
void AbstractScheduler::resume()
{
    schedule_policy_->resume();
}
void AbstractScheduler::cancel()
{
    schedule_policy_->cancel();
}
void AbstractScheduler::taskEventNotify(task::TaskEventType task_event_type)
{
    if (event_notify_)
    {
        event_notify_(task_event_type);
    }
}
void AbstractScheduler::initialize()
{
    schedule_policy_->setTaskEventNotify(std::bind(&AbstractScheduler::taskEventNotify, this, std::placeholders::_1));
}
void AbstractScheduler::setTaskEventNotify(task::TaskEventNotify event_notify)
{
    for (auto &scheduler: scheduler_vec_)
    {
        scheduler->setTaskEventNotify(event_notify);
    }
    event_notify_ = std::move(event_notify);
}
}
